
const mysql = require('mysql2');

// 创建连接池
const pool  = mysql.createPool({
    host: '127.0.0.1',
    user: 'root',
    password: '123456',
    database: 'nodejs_db',
    waitForConnections: true,
    connectionLimit: 10,
    queueLimit: 0
});

// 数据插入函数
const insert = async (data) => {
    return new Promise((resolve, reject) => {
        pool.query('insert into nodejs_db.wlw_data (deviceId, time,wd,sd) value (?, ?, ?, ? )', [data.deviceId,data.time,data.wd,data.sd], (err, results) => {
            if (err) {
                reject(err);
            } else {
                resolve(results);
            }
        });
    });
}

// 数据查询函数
const select = async () => {
    return new Promise((resolve, reject) => {

        pool.query('select * from nodejs_db.wlw_data', (err, results) => {
            if (err) {
                reject(err);
            } else {
                resolve(results);
            }
        });
    })
}

module.exports = {insert, select}
